package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
@Slf4j
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan  nursingPlan = getById( id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        List<NursingProjectPlan> nursingProjectPlans = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            nursingProjectPlanVo.setId(nursingProjectPlan.getId());
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
            nursingProjectPlanVo.setPlanId(Long.valueOf(nursingProjectPlan.getPlanId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        log.warn("{}",nursingPlanVo.getId());
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        // 从缓存中获取
        List<NursingPlan> lsit = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        // 缓存中有数据，直接返回
        if(lsit != null && lsit.size() > 0){
            return lsit;
        }
        // 缓存中没有数据，从数据库中查询
        lsit = this.list(Wrappers.< NursingPlan>lambdaQuery().eq(NursingPlan::getStatus, 1));
        // 将数据写入缓存
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, lsit);
        return lsit;
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */@Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan );
        this.save(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans))
        {
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
        }

        boolean result = iNursingProjectPlanService.saveBatch(projectPlans);
        redisTemplate.delete(CACHE_KEY_PREFIX);
       return result ? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */@Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan );
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)){
            iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });

            iNursingProjectPlanService.saveBatch(projectPlans);
        }
        boolean result = updateById(nursingPlan);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return result? 1 :0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */@Transactional

    @Override
    public int deleteNursingPlanById(Integer id)
    {
        iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        boolean result = removeById(id);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return result ? 1 : 0;
    }

}
